package com.zealfi.zxb.api.controller;

import com.zealfi.zxb.api.common.Result;
import com.zealfi.zxb.api.common.Status;
import com.zealfi.zxb.api.pojo.Cust;
import com.zealfi.zxb.api.service.ThirdPartyService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by kuan at 2017-10-16 下午 2:05
 */
@RestController
public class ThirdPartyController {
    private static final Logger logger = LoggerFactory.getLogger(ThirdPartyController.class);

    @Autowired
    private ThirdPartyService thirdPartyService;

    @RequestMapping("check")
    public ResponseEntity<Result> checkMobile(Cust cust) {
        try {
            logger.info("检查手机号" + cust.getTelNo() + "是否可用");

            if (StringUtils.isEmpty(cust.getTelNo())) {
                return illegalPhoneNumber(cust.getTelNo(), "checkMobile");
            }

            return ResponseEntity.ok(thirdPartyService.checkMobile(cust));
        } catch (Exception e) {
            logger.error("Service error,check mobile failed!", e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result(Status.SERVER_ERROR));
    }

    @RequestMapping("register")
    public ResponseEntity<Result> register(Cust cust) {
        try {
            String telNo = cust.getTelNo();
            if (StringUtils.isEmpty(telNo)) {
                return illegalPhoneNumber(telNo, "register");
            }
            String pwd = cust.getPwd();
            if (pwd == null) {
                return ResponseEntity.ok(new Result(Status.INVALID_PASSWORD));
            }

            if (thirdPartyService.register(cust) > 0) {
                return ResponseEntity.ok(new Result(Status.OK, cust));
            } else {
                return ResponseEntity.ok(new Result(Status.FAILED));
            }
        } catch (Exception e) {
            logger.error("Service error," + cust + "register failed!", e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result(Status.SERVER_ERROR));
    }

    @RequestMapping("login")
    public ResponseEntity<Result> login(Cust cust) {
        try {
            String telNo = cust.getTelNo();
            if (StringUtils.isEmpty(telNo)) {
                return illegalPhoneNumber(telNo, "login");
            }
            String pwd = cust.getPwd();
            if (pwd == null) {
                return ResponseEntity.ok(new Result(Status.INVALID_PASSWORD));
            }

            if (thirdPartyService.login(cust) > 0) {
                return ResponseEntity.ok(new Result(Status.OK, cust));
            } else {
                return ResponseEntity.ok(new Result(Status.ACCOUNT_OR_PASSWORD_ERROR));
            }
        } catch (Exception e) {
            logger.error("Service error," + cust + "login failed!", e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result(Status.SERVER_ERROR));
    }

    @RequestMapping("changePassword")
    public ResponseEntity<Result> changePassword(Cust cust) {
        try {
            String telNo = cust.getTelNo();
            if (StringUtils.isEmpty(telNo)) {
                cust.setTelNo(null);
            }
            if (cust.getId() == null) {
                return illegalPhoneNumber(telNo, "changePassword");
            }
            String pwd = cust.getPwd();
            if (pwd == null) {
                return ResponseEntity.ok(new Result(Status.INVALID_PASSWORD));
            }

            if (thirdPartyService.changePassword(cust) > 0) {
                return ResponseEntity.ok(new Result(Status.OK));
            } else {
                return ResponseEntity.ok(new Result(Status.FAILED));
            }
        } catch (Exception e) {
            logger.error("Service error," + cust + "change password failed!", e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result(Status.SERVER_ERROR));
    }

    /**
     * 非法的手机号码
     */
    private ResponseEntity<Result> illegalPhoneNumber(String mobile, String target) {
        logger.info("非法的手机号码:" + mobile + ",不允许使用'" + target + "'");

        return ResponseEntity.ok(new Result(Status.NOT_MOBILE_NUMBER));
    }
}
